<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'title' => 'SimpleRandomSentences.java',
	'subtitle' => 'Written in <span title="Programming 2">CS 1103</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-06-28',
	'copyright year' => '2017',
	'body' => <<<END
<h2>Source code:</h2>
<blockquote>
<pre><code>/* 
 * Copyright (C) 2017 Alex Yst
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
 */
package st.y.CS1103.Unit2;

/**
 * Alex Yst&apos;s CS 1103 (Programming 2) Unit 2 submission
 * 
 * This class outputs random sentences. See the documentation of the main()
 * method for details.
 * 
 * @author Alex Yst
 */
public class SimpleRandomSentences {
/**
 * This is an array of possible conjunctions for use in sentences.
 */
	public static String[] conjunction = {
		&quot;and&quot;,
		&quot;or&quot;,
		&quot;but&quot;,
		&quot;because&quot;,
	};
/**
 * This is an array of possible proper nouns for use in sentences.
 */
	public static String[] properNoun = {
		&quot;Fred&quot;,
		&quot;Jane&quot;,
		&quot;Richard Nixon&quot;,
		&quot;Miss America&quot;,
	};
	/**
	 * This is an array of possible common nouns for use in sentences.
	 */
	public static String[] commonNoun = {
		&quot;man&quot;,
		&quot;woman&quot;,
		&quot;fish&quot;,
		&quot;elephant&quot;,
		&quot;unicorn&quot;,
	};
	/**
	 * This is an array of possible determiners for use in sentences.
	 */
	public static String[] determiner = {
		&quot;a&quot;,
		&quot;the&quot;,
		&quot;every&quot;,
		&quot;some&quot;,
	};
	/**
	 * This is an array of possible adjectives for use in sentences.
	 */
	public static String[] adjective = {
		&quot;big&quot;,
		&quot;tiny&quot;,
		&quot;pretty&quot;,
		&quot;bald&quot;,
	};
	/**
	 * This is an array of possible intransitive verbs for use in sentences.
	 */
	public static String[] intransitiveVerb = {
		&quot;runs&quot;,
		&quot;jumps&quot;,
		&quot;talks&quot;,
		&quot;sleeps&quot;,
	};
	/**
	 * This is an array of possible transitive verbs for use in sentences.
	 */
	public static String[] transitiveVerb = {
		&quot;loves&quot;,
		&quot;hates&quot;,
		&quot;sees&quot;,
		&quot;knows&quot;,
		&quot;looks for&quot;,
		&quot;finds&quot;,
	};
/**
 * This method chooses one string from an array with equal probability.
 * 
 * @param listOfStrings An array of strings
 * @return
 */
	public static String randomItem(String[] listOfStrings) {
		return listOfStrings[(int) (Math.random() * listOfStrings.length)];
	}
/**
 * This method generates a random sentence.
 * 
 * @return A sentence
 */
	public static String sentence() {
		if(Math.random() &lt; 0.7) {
			return simple_sentence();
		} else {
			return simple_sentence() + &quot; &quot; + randomItem(conjunction) + &quot; &quot; + sentence();
		}
	}
/**
 * This method generates a random sentence without a conjunction.
 * 
 * @return A simple, random sentence
 */
	public static String simple_sentence() {
		return noun_phrase() + &quot; &quot; + verb_phrase();
	}
/**
 * This method generates a random noun phrase.
 * 
 * @return A noun phrase
 */
	public static String noun_phrase() {
		double rand = Math.random();
		String adjective_string = &quot;&quot;;
		while(Math.random() &lt; 0.1) {
			adjective_string += randomItem(adjective) + &quot; &quot;;
		}
		if(rand &gt; 0.2) {
			return randomItem(properNoun);
		} else if(rand &lt; 0.6) {
			return randomItem(determiner) + &quot; &quot; + adjective_string + randomItem(commonNoun);
		} else {
			return randomItem(determiner) + &quot; &quot; + adjective_string + randomItem(commonNoun) + &quot; who &quot; + verb_phrase();
		}
	}
/**
 * This method generates a random verb phrase.
 * 
 * @return A verb phrase
 */
	public static String verb_phrase() {
		double rand = Math.random();
		if(rand &lt; 0.25) {
			return randomItem(intransitiveVerb);
		} else if(rand &lt; 0.5) {
			return randomItem(transitiveVerb) + &quot; &quot; + noun_phrase();
		} else if(rand &lt; 0.75) {
			return &quot;is &quot; + randomItem(adjective);
		} else {
			return &quot;believes that &quot; + simple_sentence();
		}
	}
/**
 * This program generates several random sentences. Unfortunately, these
 * sentences lack proper capitalisation and punctuation.
 * 
 * @param arguments Command line arguments are ignored.
 */
	public static void main(String[] arguments) {
		for(int i=0;i&lt;16;i++) {
			System.out.println(sentence());
		}
	}
}</code></pre>
</blockquote>
END
);
